package logic

import (
	"context"
	"gorm.io/gorm"
	"shuqi-novel/model/mysql"

	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *shuqirpc.UpdateRequest) (*shuqirpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.Book{
		Model:   gorm.Model{ID: uint(in.BookId)},
		Name:    in.Name,
		Title:   in.Title,
		Context: in.Context,
		UserId:  int(in.UserId),
	}
	err := user.UpdateBook(mysql.DB, in.BookId)
	if err != nil {
		return nil, err
	}
	return &shuqirpc.UpdateResponse{
		BookId: int64(user.ID),
	}, nil
}
